﻿using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.DataUpdater;
using AgencyManagerSystem.Model;
using System;

namespace AgencyManagerSystem.Core
{
    public class RetailMemberUpdaterCreator : BaseUpdaterCreator<RetailMemberModel>
    {
        RetailMemberFetcher retailMemberFetcher = new RetailMemberFetcher();
        public RetailMemberUpdater CreateRetailUpdaterInstance()
        {
            RetailMemberUpdater retailMemberUpdater = new RetailMemberUpdater(retailMemberFetcher);
            retailMemberUpdater.DataUpdating += FlagAndApproveFlagValid;
            retailMemberUpdater.DataUpdating += NotSpaceCheck;
            retailMemberUpdater.DataUpdating += ReferExistCheck;
            retailMemberUpdater.DataUpdating += CodeUniqueCheck;
            retailMemberUpdater.DataUpdating += UserUniqueCheck;
            return retailMemberUpdater;
        }

        private void UserUniqueCheck(BaseUpdater<RetailMemberModel> sender, DataUpdatingEventArgs<RetailMemberModel> e)
        {
            SupplierMemberFetcher suppilerMemberFetcher = new SupplierMemberFetcher();
            var model = suppilerMemberFetcher.GetByUser(e.Insert.User);
            if (model != null && model.AutoId != e.Insert.AutoId)
            {
                e.Cancel = true;
                e.Message = String.Format("成员{0}已存在",model.Code);
                return;
            }
        }

        private void CodeUniqueCheck(BaseUpdater<RetailMemberModel> sender, DataUpdatingEventArgs<RetailMemberModel> e)
        {
            if (e.Insert == null)
                return;
            RetailMemberFetcher retailMemberFetcher = new RetailMemberFetcher();
            var model = retailMemberFetcher.GetByCode(e.Insert.Retail, e.Insert.Code);
            if (model != null && model.AutoId != e.Insert.AutoId)
            {
                e.Cancel = true;
                e.Message = String.Format("编号{0}已存在", model.Code,model.AutoId);
                return;
            }
        }

        private void ReferExistCheck(BaseUpdater<RetailMemberModel> sender, DataUpdatingEventArgs<RetailMemberModel> e)
        {
            if (e.Insert == null)
                return;
            UserFetcher userFetcher = new UserFetcher();
            RetailFetcher retailFetcher = new RetailFetcher();
            if (retailFetcher.Get(e.Insert.Retail) == null)
            {
                e.Cancel = true;
                e.Message = String.Format("对应经销商{0}不存在",e.Insert.Retail);
                return;
            }
            if (userFetcher.Get(e.Insert.User) == null)
            {
                e.Cancel = true;
                e.Message = String.Format("对应用户{0}不存在",e.Insert.User);
                return;
            }
        }

        private void NotSpaceCheck(BaseUpdater<RetailMemberModel> sender, DataUpdatingEventArgs<RetailMemberModel> e)
        {
            if (e.Insert == null)
                return;
            if (String.IsNullOrWhiteSpace(e.Insert.Code) || String.IsNullOrWhiteSpace(e.Insert.Name))
            {
                e.Cancel = true;
                e.Message = "代号或名称不能为空";
                return;
            }
        }
    }
}
